import utils from '../utils'
import cli from '../cli'

function mapFieldNames(names) {
  return (names || []).reduce((memo, str) => {
    const parts = str.split('=')
    const dest = cli.trimQuotes(parts[0])
    const src = parts.length > 1 ? cli.trimQuotes(parts[1]) : dest
    if (!src || !dest) {
      // stop("Invalid field description:", str)
      utils.error(`Invalid field description:${src}`)
    }
    memo[src] = dest
    return memo
  }, {})
}
function getRecordMapper(map) {
  const fields = Object.keys(map)
  return (src) => {
    const dest = {}
    let key = null
    for (let i=0, n=fields.length; i<n; i++) {
      key = fields[i]
      dest[map[key]] = src[key]
    }
    return dest
  }
}
export default {
  mapFieldNames,
  getRecordMapper,
}